<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Example: Constrain Support</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
    <link rel="stylesheet" href="../assets/css/main.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <script src="../../build/yui/yui-min.js"></script>
</head>
<body>

<div id="doc">
    <h1>Example: Constrain Support</h1>

    

    <div class="yui3-g">
        <div id="main" class="yui3-u">
            <div class="content"><style type="text/css" scoped>

/* Overlay Look/Feel */
.yui3-overlay-content {
    text-align: center;
    padding: 2px;
    border: 1px solid #000;
    background-color: #aaa;
    font-size: 93%;
}

.yui3-overlay-content .yui3-widget-hd {
    font-weight: bold;
    padding: 5px;
    border: 2px solid #aa0000;
    background-color: #fff;
}

.yui3-overlay-content .yui3-widget-ft {
    padding: 5px;
    border: 2px solid #000;
    background-color: #ccc;
}

.yui3-overlay-content .yui3-widget-bd {
    padding: 5px;
    border: 2px solid #0000aa;
    background-color: #fff;
}

/* Example Layout CSS */
.overlay-example {
    position: relative;
    height: 450px;
    width: 550px;
}

#constrain-box {
    border: 1px solid #000;
    background-color: #004C6D;

    position: relative;

    top: 80px;
    left: 80px;

    width: 350px;
    height: 300px;
}

.yui3-slider {
    position: absolute;
    background-color: transparent;
}

#x {
    position: absolute;
    top: 10px;
    left: 30px;
}

#y {
    position: absolute;
    top: 30px;
    left: 10px;
}

</style>

<div class="intro">
    <p>
    This example shows how you can use Overlay's constrained positioning support to constrain the XY position of the overlay so that it remains within another node on the page (or within the viewport).
    </p>
</div>

<div class="example">
    <div class="overlay-example yui3-skin-sam" id="overlay-example">
    <div id="constrain-box"></div>
</div>

<script type="text/javascript">
YUI().use("overlay", "slider", function (Y) {

    var constrainRegion = Y.one("#constrain-box").get("region"),
        overlay, sx, yx, checkbox;

    // Create Overlay from script.
    overlay = new Y.Overlay({
        id: "overlay",

        width : "140px",
        height: "120px",

        headerContent: "Constrained",
        bodyContent  : "Use the sliders to move the overlay",
        footerContent: '<label><input type="checkbox" id="constrain"> Constrain </label>',

        constrain: "#constrain-box",
        align    : {
            node  : "#constrain-box",
            points: ["cc", "cc"]
        },

        render: "#overlay-example"
    });

    // Create horizontal Slider.
    sx = new Y.Slider({
        id    : "x",
        length: "450px",
        min   : constrainRegion.left - 50,
        max   : constrainRegion.right + 50,
        value : overlay.get("x"),
        render: "#overlay-example"
    });

    // Create vertical Slider.
    sy = new Y.Slider({
        id    : "y",
        axis  : 'y',
        length: "400px",
        min   : constrainRegion.top - 50, 
        max   : constrainRegion.bottom + 50,
        value : overlay.get("y"),
        render: "#overlay-example"
    });

    sx.after("valueChange", function (e) {
        overlay.set("x", e.newVal);
    });

    sy.after("valueChange", function (e) {
        overlay.set("y", e.newVal);
    });

    function enableConstraints (constrain) {
        if (constrain) {
            overlay.set("constrain", "#constrain-box");
            overlay.set("headerContent", "Constrained");
        } else {
            overlay.set("constrain", false);
            overlay.set("headerContent", "Unconstrained");
        }

        // Sync the current values of the sliders to the overlay.
        overlay.set("xy", [sx.get("value"), sy.get("value")]);
    }

    // Reference <code>&lt;input type=&quot;checkbox&quot; &#x2F;&gt;</code> from the Overlay's footer.
    checkbox = Y.one("#constrain");
    checkbox.set("checked", true);
    checkbox.on("click", function (e) {
        enableConstraints(this.get("checked"));
    });

    // Enable constraints by default.
    enableConstraints(true);

});
</script>

</div>

<h2>Overlay Constrained XY Positioning</h2>

<h3>Setting Up The YUI Instance</h3>

<p>
As with the <a href="overlay-xy.html">"Basic XY Positioning"</a> example, to create an instance of an Overlay on your page, the only module you need to request is the <code>overlay</code> module. The <code>overlay</code> module will pull in the <code>widget</code>, <code>widget-stack</code>, <code>widget-position</code>, <code>widget-position-align</code>, <code>widget-position-constrain</code> and <code>widget-stdmod</code> extensions it uses.
</p>

<pre class="code prettyprint">YUI({...}).use(&quot;overlay&quot;, function (Y) {
    &#x2F;&#x2F; We&#x27;ll write example code here, where we have a Y.Overlay class 
    &#x2F;&#x2F; available, which is bundled with XY positioning, align and 
    &#x2F;&#x2F; constrain support.
});</pre>


<p>
Using the <code>overlay</code> module, will also pull down the default CSS required for overlay, on top of which we only need to add our required look/feel CSS for the example.
</p>

<h3>Instantiating The Overlay</h3>

<p>
For this example, we'll instantiate an Overlay from script, as we did for the <a href="overlay-align.html">"Alignment Support"</a> example, but use the <code>render</code> attribute to specify the node under which we want the Overlay to be rendered in the DOM, and to indicate that we want it rendered on construction. The <code>render</code> attribute is a sugar attribute for the <code>render()</code> method:
</p>

<pre class="code prettyprint">&#x2F;&#x2F; Create Overlay from script.
overlay = new Y.Overlay({
    id: &quot;overlay&quot;,

    width : &quot;140px&quot;,
    height: &quot;120px&quot;,

    headerContent: &quot;Constrained&quot;,
    bodyContent  : &quot;Use the sliders to move the overlay&quot;,
    footerContent: &#x27;&lt;label&gt;&lt;input type=&quot;checkbox&quot; id=&quot;constrain&quot;&gt; Constrain &lt;&#x2F;label&gt;&#x27;,

    constrain: &quot;#constrain-box&quot;,
    align    : {
        node  : &quot;#constrain-box&quot;,
        points: [&quot;cc&quot;, &quot;cc&quot;]
    },

    render: &quot;#overlay-example&quot;
});</pre>


<p>
We align the overlay to the center of the <code>#constrain-box</code> node, which we're also using as the constraining node for the overlay. The <code>constrain</code> attribute accepts a node reference (either an actual Node instance, or a string selector reference), or it can simply be set to <code>true</code> to constrain the overlay to the Viewport.
</p>

<h3>Demonstrating Constrained Support</h3>

<p>
For this example, we set up a couple of Slider instances which can be used to set the Overlay's <code>x</code> and <code>y</code> attribute values. 
</p>

<pre class="code prettyprint">&#x2F;&#x2F; Get the region for the constraining box.
var constrainRegion = Y.one(&quot;#constrain-box&quot;).get(&quot;region&quot;);

&#x2F;&#x2F; Use the Overlay&#x27;s current &#x60;x&#x60; and &#x60;y&#x60; to set the initial Slider values.

&#x2F;&#x2F; Create horizontal Slider.
sx = new Y.Slider({
    id    : &quot;x&quot;,
    length: &quot;450px&quot;,
    min   : constrainRegion.left - 50,
    max   : constrainRegion.right + 50,
    value : overlay.get(&quot;x&quot;),
    render: &quot;#overlay-example&quot;
});

&#x2F;&#x2F; Create vertical Slider.
sy = new Y.Slider({
    id    : &quot;y&quot;,
    axis  : &#x27;y&#x27;,
    length: &quot;400px&quot;,
    min   : constrainRegion.top - 50, 
    max   : constrainRegion.bottom + 50,
    value : overlay.get(&quot;y&quot;),
    render: &quot;#overlay-example&quot;
});</pre>


<p>
We set the <code>min</code> and <code>max</code> values of the slider instances to allow the overlay to be moved beyond the edge of the constraining region, and set the initial <code>value</code> of the sliders to reflect the current centered position of the overlay.
</p>

<p>
Finally, we set up <code>valueChange</code> listeners for the sliders, when attempt to set the corresponding axis position of the overlay:
</p>

<pre class="code prettyprint">&#x2F;&#x2F; Set the Overlay&#x27;s &#x60;x&#x60; attribute value.
sx.after(&quot;valueChange&quot;, function (e) {
    overlay.set(&quot;x&quot;, e.newVal);
});

&#x2F;&#x2F; Set the Overlay&#x27;s &#x60;y&#x60; attribute value.
sy.after(&quot;valueChange&quot;, function (e) {
    overlay.set(&quot;y&quot;, e.newVal);
});</pre>


<h3>CSS: Overlay Look/Feel</h3>

<p>
As mentioned in the <a href="overlay-xy.html">"Basic XY Positioning"</a> example, the overlay.css Sam Skin file (build/overlay/assets/skins/sam/overlay.css) provides the default functional CSS for the overlay. Namely the CSS rules to hide the overlay, and position it absolutely. However there's no default out-of-the-box look/feel applied to the Overlay widget.
</p>

<p>
The example provides it's own look and feel for the Overlay, by defining rules for the content box, header and body sections:
</p>

<pre class="code prettyprint">&#x2F;* Overlay Look&#x2F;Feel *&#x2F;
.yui3-overlay-content {
    text-align: center;
    padding: 2px;
    border: 1px solid #000;
    background-color: #aaa;
    font-size: 93%;
}

.yui3-overlay-content .yui3-widget-hd {
    font-weight: bold;
    padding: 5px;
    border: 2px solid #aa0000;
    background-color: #fff;
}

.yui3-overlay-content .yui3-widget-ft {
    padding: 5px;
    border: 2px solid #000;
    background-color: #ccc;
}

.yui3-overlay-content .yui3-widget-bd {
    padding: 5px;
    border: 2px solid #0000aa;
    background-color: #fff;
}</pre>


<h2>Complete Example Source</h2>

<pre class="code prettyprint">&lt;div class=&quot;overlay-example yui3-skin-sam&quot; id=&quot;overlay-example&quot;&gt;
    &lt;div id=&quot;constrain-box&quot;&gt;&lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;

&lt;script type=&quot;text&#x2F;javascript&quot;&gt;
YUI().use(&quot;overlay&quot;, &quot;slider&quot;, function (Y) {

    var constrainRegion = Y.one(&quot;#constrain-box&quot;).get(&quot;region&quot;),
        overlay, sx, yx, checkbox;

    &#x2F;&#x2F; Create Overlay from script.
    overlay = new Y.Overlay({
        id: &quot;overlay&quot;,

        width : &quot;140px&quot;,
        height: &quot;120px&quot;,

        headerContent: &quot;Constrained&quot;,
        bodyContent  : &quot;Use the sliders to move the overlay&quot;,
        footerContent: &#x27;&lt;label&gt;&lt;input type=&quot;checkbox&quot; id=&quot;constrain&quot;&gt; Constrain &lt;&#x2F;label&gt;&#x27;,

        constrain: &quot;#constrain-box&quot;,
        align    : {
            node  : &quot;#constrain-box&quot;,
            points: [&quot;cc&quot;, &quot;cc&quot;]
        },

        render: &quot;#overlay-example&quot;
    });

    &#x2F;&#x2F; Create horizontal Slider.
    sx = new Y.Slider({
        id    : &quot;x&quot;,
        length: &quot;450px&quot;,
        min   : constrainRegion.left - 50,
        max   : constrainRegion.right + 50,
        value : overlay.get(&quot;x&quot;),
        render: &quot;#overlay-example&quot;
    });

    &#x2F;&#x2F; Create vertical Slider.
    sy = new Y.Slider({
        id    : &quot;y&quot;,
        axis  : &#x27;y&#x27;,
        length: &quot;400px&quot;,
        min   : constrainRegion.top - 50, 
        max   : constrainRegion.bottom + 50,
        value : overlay.get(&quot;y&quot;),
        render: &quot;#overlay-example&quot;
    });

    sx.after(&quot;valueChange&quot;, function (e) {
        overlay.set(&quot;x&quot;, e.newVal);
    });

    sy.after(&quot;valueChange&quot;, function (e) {
        overlay.set(&quot;y&quot;, e.newVal);
    });

    function enableConstraints (constrain) {
        if (constrain) {
            overlay.set(&quot;constrain&quot;, &quot;#constrain-box&quot;);
            overlay.set(&quot;headerContent&quot;, &quot;Constrained&quot;);
        } else {
            overlay.set(&quot;constrain&quot;, false);
            overlay.set(&quot;headerContent&quot;, &quot;Unconstrained&quot;);
        }

        &#x2F;&#x2F; Sync the current values of the sliders to the overlay.
        overlay.set(&quot;xy&quot;, [sx.get(&quot;value&quot;), sy.get(&quot;value&quot;)]);
    }

    &#x2F;&#x2F; Reference &#x60;&lt;input type=&quot;checkbox&quot; &#x2F;&gt;&#x60; from the Overlay&#x27;s footer.
    checkbox = Y.one(&quot;#constrain&quot;);
    checkbox.set(&quot;checked&quot;, true);
    checkbox.on(&quot;click&quot;, function (e) {
        enableConstraints(this.get(&quot;checked&quot;));
    });

    &#x2F;&#x2F; Enable constraints by default.
    enableConstraints(true);

});
&lt;&#x2F;script&gt;</pre>

</div>
        </div>

        <div id="sidebar" class="yui3-u">
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                                    <li data-description="Shows how to instantiate a basic Overlay instance, and use the Overlay&#x27;s basic XY positioning support.">
                                        <a href="overlay-xy.html">Basic XY Positioning</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Shows how to use the Overlay&#x27;s XY alignment support, to align the Overlay relative to another element, or to the viewport.">
                                        <a href="overlay-align.html">Alignment Support</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Shows how to use the Overlay&#x27;s zindex and shim support when positioning Overlays above other elements on the page.">
                                        <a href="overlay-stack.html">Stack Support</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Shows how to modify content in the Overlay&#x27;s header, body and footer sections.">
                                        <a href="overlay-stdmod.html">Standard Module Support</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Shows how to use Overlay&#x27;s constrainment support, to limit the XY value which can be set for an Overlay.">
                                        <a href="overlay-constrain.html">Constrain Support</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Shows how to create a simple plugin to retrieve content for the Overlay using the io utility.">
                                        <a href="overlay-io-plugin.html">IO Plugin</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Shows how to create a simple plugin to animate the Overlay&#x27;s movement and visibility.">
                                        <a href="overlay-anim-plugin.html">Animation Plugin</a>
                                    </li>
                                
                            
                                
                            
                                
                            
                        </ul>
                    </div>
                </div>
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples That Use This Component</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                                    <li data-description="Creating an accessible menu button using the Focus Manager Node Plugin, Event&#x27;s delegation support and mouseenter event, along with the Overlay widget and Node&#x27;s support for the WAI-ARIA Roles and States.">
                                        <a href="../node-focusmanager/node-focusmanager-3.html">Accessible Menu Button</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Use StyleSheet to adjust the CSS rules applying a page theme from user input">
                                        <a href="../stylesheet/stylesheet-theme.html">Adjusting a Page Theme on the Fly</a>
                                    </li>
                                
                            
                        </ul>
                    </div>
                </div>
            
        </div>
    </div>
</div>

<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>

</body>
</html>
